package com.onlinevideo.dao.impl;


import com.onlinevideo.dao.BaseDao;
import com.onlinevideo.dao.RoleAuthorityDao;
import com.onlinevideo.entity.*;

import java.util.ArrayList;
import java.util.List;

public class RoleAuthorityDaoImpl extends BaseDao implements RoleAuthorityDao {


    @Override
    public int insert(RoleAuthority roleAuthority) {
        String sql ="INSERT INTO `T_role_authority` (`role_id`,`authority_id`) VALUES (?,?)";
        return super.update(sql,roleAuthority.getRoleId(),roleAuthority.getAuthorityId());
    }

    @Override
    public int delete(Long id) {
        String sql = "DELETE FROM `T_role_authority` WHERE `id`=?";
        return super.update(sql,id);
    }


    @Override
    public  List<RoleAuthorityDTO> queryByRoleId(Long roleId) {
        List<RoleAuthorityDTO> roleAuthorityDTOS = new ArrayList<>();
        //查Role_Authority
        String sql1 = "SELECT `id`,`role_id` AS 'roleId',`authority_id` AS `authorityId` FROM  `t_role_authority`   WHERE 1=1 AND `role_id` = ?  ";
        List<RoleAuthority> roleAuthorities = super.queryForList(RoleAuthority.class, sql1, roleId);
        //查Role
        String sql2 = "SELECT `id`,`role_name` AS `roleName`,`role_description` AS `roleDescription`,`role_status` AS `roleStatus` FROM `T_role` WHERE 1=1 AND `id`=?  ";
        Role role = super.queryForOne(Role.class, sql2, roleId);
        //查Authority
        String sql3 = "SELECT `id`,`url`,`authority_description` AS `authorityDescription` FROM `T_authority`  WHERE 1=1 AND  `id` = ? ";
        for (RoleAuthority roleAuthority : roleAuthorities) {
            roleAuthorityDTOS.add(new RoleAuthorityDTO(roleAuthority.getId(), role, super.queryForOne(Authority.class, sql3, roleAuthority.getAuthorityId())));
        }
        return roleAuthorityDTOS;
    }

    @Override
    public List<RoleAuthority> queryAll() {
        String sql = "SELECT `id`,`role_id` AS 'roleId',`authority_id` AS `authorityId` FROM  `t_role_authority` ";
        return super.queryForList(RoleAuthority.class, sql);
    }

    @Override
    public List<RoleAuthority> queryAllByStartIndexAndPageSize(Integer startIndex, Integer pageSize) {
        String sql = "SELECT `id`,`role_id` AS 'roleId',`authority_id` AS `authorityId` FROM  `t_role_authority` ";
        return super.queryForList(RoleAuthority.class, sql);
    }

}
